package com.jinyang.jetpackdemo.activity

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.BindingAdapter
import androidx.databinding.BindingConversion
import androidx.databinding.DataBindingUtil
import coil.load
import com.jinyang.jetpackdemo.R
import com.jinyang.jetpackdemo.bean.ArticleItem4
import com.jinyang.jetpackdemo.bean.ImageBean
import com.jinyang.jetpackdemo.databinding.ActivityArticleListBinding
import com.jinyang.jetpackdemo.util.LjyLogUtil

class ArticleListActivity : AppCompatActivity() {
    lateinit var articleInfo: ArticleItem4
    lateinit var image: ImageBean
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityArticleListBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_article_list)
        binding.lifecycleOwner = this
        articleInfo = ArticleItem4(
            "Android Jetpack系列", "今阳",
            "Jetpack 是一个由多个库组成的套件;"
        )
        binding.articleInfo = articleInfo
        image = ImageBean("https://pic1.zhimg.com/v2-dc32dcddfd7e78e56cc4b6f689a24979_is.jpg")
        binding.image=image
        binding.onClickPresenter = OnClickPresenter()
    }

    inner class OnClickPresenter {

        fun changeTitle(articleInfo: ArticleItem4) {
            articleInfo.title.value += "6"
            image.url.value="https://pic3.zhimg.com/v2-e5656460688d19f7358ab3a6055fe34a_720w.jpg?source=95cc6b4a"
        }

        fun changeAuthor() {
            articleInfo.author.value += "6"
            image.url.value="https://pic2.zhimg.com/v2-f6981776beae87401991b426fbe34fdd_720w.jpg?source=95cc6b4a"
        }

        fun changeContent() {
            articleInfo.content.value += "6"
            image.url.value="https://pic2.zhimg.com/v2-f2eddc2fe0e509de5bbeeb351ddc2c61_1440w.jpg?source=172ae18b"
        }
    }

}

@BindingAdapter("android:text")
fun setText(view: Button, text: String) {
    view.text = "$text-Button"
}
@BindingAdapter("url")
fun loadImage(view: ImageView, url: String) {
    view.load(url)
    LjyLogUtil.d("url:${url}")
}

@BindingConversion
fun convertStringToDrawable(str: String): Drawable {
    return when (str) {
        "红色" -> {
            ColorDrawable(Color.parseColor("#FF4081"))
        }
        "蓝色" -> {
            ColorDrawable(Color.parseColor("#3F51B5"))
        }
        else -> {
            ColorDrawable(Color.parseColor("#344567"))
        }
    }
}

@BindingConversion
fun convertStringToColor(str: String): Int {
    return when (str) {
        "红色" -> {
            Color.parseColor("#FF4081")
        }
        "蓝色" -> {
            Color.parseColor("#3F51B5")
        }
        else -> {
            Color.parseColor("#344567")
        }
    }
}

